﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Navigation;
using GTasksPlus.Code;
using Microsoft.Phone.Controls;

namespace GTasksPlus
{
    public partial class Login
    {
        public Login()
        {
            InitializeComponent();
            if (!GTasksSettings.IsLoggedIn())
                Dispatcher.BeginInvoke(() => this.objAuthorizeBrowserControl.Navigate(OAuthHelper.GetLoginUrl()));
            Loaded += MainPageLoaded;
        }

        private void MainPageLoaded(object sender, RoutedEventArgs e)
        {
            ApplicationTitle.Text = GTasksSettings.ApplicationTitle;
            if (GTasksSettings.IsLoggedIn() && NavigationService != null)
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }

        private void ObjAuthorizeBrowserControlNavigating(object sender, NavigatingEventArgs e)
        {
            SetVisibility(false);
            if (e.Uri.ToString().Contains("code="))
            {
                e.Cancel = true;
                var val = e.Uri.Query.Substring(6, e.Uri.Query.Length - 6);
                objAuthorizeBrowserControl.Visibility = Visibility.Collapsed;
                GTasksSettings.TokenCode = val;
                OAuthHelper.GetRequestTokenQuery(false, InspectTokenCallback);
            }
        }

        private void InspectTokenCallback(List<object> token)
        {
            Dispatcher.BeginInvoke(() =>
                                       {
                                           if (!string.IsNullOrEmpty(token[0] as string))
                                           {
                                               NavigationService.Navigate(
                                                   new Uri(
                                                       "/MainPage.xaml",
                                                       UriKind.
                                                           RelativeOrAbsolute));
                                           }
                                       });
        }

        private void objAuthorizeBrowserControlNavigated(object sender, NavigationEventArgs e)
        {
            SetVisibility(true);
        }

        public void SetVisibility(bool value)
        {
            if (value)
            {
                performanceProgressBar.Visibility = Visibility.Collapsed;
                objAuthorizeBrowserControl.Visibility = Visibility.Visible;
            }
            else
            {
                performanceProgressBar.Visibility = Visibility.Visible;
                objAuthorizeBrowserControl.Visibility = Visibility.Collapsed;
            }
        }

        private void Login_Click(object sender, EventArgs e)
        {
            objAuthorizeBrowserControl.Navigate(OAuthHelper.GetLoginUrl());
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            while (NavigationService.CanGoBack)
                NavigationService.RemoveBackEntry();
        }

        private void About_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/About.xaml", UriKind.RelativeOrAbsolute));
        }
    }
}